package org.c41.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import org.c41.system.ICloseable;

public class UDPSocket implements ICloseable{

	private DatagramSocket socket;
	
	public UDPSocket(int port) throws SocketException{
		this.socket=new DatagramSocket(port);
	}
	
	public UDPSocket() throws SocketException{
		this.socket=new DatagramSocket();
	}
	
	public void send(UDPPacket packet) throws IOException{
		socket.send(packet.packet);
	}
	
	public UDPPacket receive(byte[] buff) throws IOException{
		DatagramPacket packet = new DatagramPacket(buff, buff.length);
		socket.receive(packet);
		return new UDPPacket(packet);
	}

	@Override
	public void close() throws IOException {
		socket.close();
	}

	@Override
	public boolean isClosed() {
		return socket.isClosed();
	}
	
}
